<template>
	<view class="container">
		<view class="nav">
			<text @tap="changePage(0)" :class="{active:index==0}">精选文章</text>
			<text @tap="changePage(1)" :class="{active:index==1}">品牌列表</text>
		</view>
		<view class="container-c">
			<view v-if="index==1" class="brands" v-for="item in list" :key="item.id">
				<image :src="item.bigPic" mode=""></image>
				<view class="text">
					<view class="name">
						{{item.name}}
					</view>
					<view class="desc">
						{{item.brandStory}}
					</view>
				</view>
			</view>
			<view v-if="index==0" v-for="item in articleList" :key="item.id" class="article">
				<image :src="item.coverImg" @tap="goArticleInfo(item.id)" mode=""></image>
				<view class="content">
					<text class="title">{{item.title}}</text>
					<text class="author">{{item.author}}</text>
				</view>
				<view class="watch">
					<view class="left">
						<image class="view" src="../../static/icons/view.png" mode=""></image>
						<text>{{item.viewCount}}</text>
					</view>
					<uni-icons :color="collected==true?'red':''"  type="heart" size="24"></uni-icons>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		findArticleByPage,
		findBrandsByPage
	} from "@/api/find/index.js"
	export default {
		data() {
			return {
				list: [],
				index: 0,
				articleList: [],
				collected: false
			};
		},
		methods: {
			changePage(bool) {
				if (bool == 0) {
					this.index = 0
				} else if (bool == 1) {
					this.index = 1
				}
			},
			goArticleInfo(id) {
				uni.navigateTo({
					url: "/pages/find/articleInfo/articleInfo?id=" + id
				})
			},
			// collectArticle() {
			// 	console.log()
			// 	console.log(this.articleList)
			// 	var collectArticleList = uni.getStorageSync("collectedarticle");
			// 	if (collectArticleList) {
			// 		var item = collectArticleList.find(ele => ele.id == this.articleList.id);
			// 		if (item) {
			// 			var index = collectArticleList.findIndex(ele => ele.id == this.articleList.id);
			// 			collectArticleList.splice(index, 1);		
			// 			uni.setStorageSync("collectedarticle", collectArticleList)
			// 			this.collected=false;
			// 		} else {
			// 			collectArticleList.push(this.articleList);
			// 			uni.setStorageSync("collectedarticle", collectArticleList)
			// 			this.collected=true;
			// 		}
			// 	} else {
			// 		var arr = [];
			// 		arr.push(this.articleList);
			// 		uni.setStorageSync("collectedarticle", arr)
			// 		this.collected=true;
			// 	}
			// }
		},
		onLoad() {
			findBrandsByPage(1, 10).then(res => {
				// console.log(res)
				this.list = res.data.rows;
			})
			findArticleByPage(1, 10).then(res => {
				console.log(res)
				this.articleList = res.data.rows;
			})
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		width: 100%;
		background-color: #f1ece7;
		// height: 100vh;
	}

	.container-c {
		width: 660rpx;
		margin: 0 auto;
		padding-bottom: 40rpx;
		border-radius: 8rpx;
	}

	.text {
		margin-top: -10rpx;
		background-color: #fff;
		text-indent: 1em;
		padding-bottom: 20rpx;

		.name {
			font-size: 36rpx;
			line-height: 44rpx;
		}

		.desc {
			font-size: 22rpx;
			color: #b1b1b1;
		}
	}

	image {
		width: 100%;
	}

	.nav {
		width: 100%;
		height: 88rpx;
		background-color: #354e44;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #fff;

		text {
			margin-right: 30rpx;
			border-bottom: 2rpx transparent #fff;
			padding-bottom: 10rpx;
		}
	}

	.active {
		border-bottom: 2rpx solid #fff;
	}

	.article {
		position: relative;

		image {
			border-radius: 40rpx 40rpx 0rpx 0rpx;
			width: 100%;
			height: 380rpx;
		}

		.content {
			display: flex;
			flex-direction: column;
			position: absolute;
			bottom: 140rpx;
			left: 20rpx;
			color: #fff;

			.title {
				font-size: 48rpx;
				line-height: 68rpx;
			}

			.author {
				font-size: 30rpx;
			}
		}

		.watch {
			display: flex;
			justify-content: space-around;
			align-items: center;
			width: 100%;
			height: 100rpx;
			background-color: #fff;
			margin-top: -2rpx;
			border-radius: 0rpx 0rpx 40rpx 40rpx;

			.view {
				width: 40rpx;
				height: 40rpx;
				margin-right: 30rpx;
				vertical-align: middle;
			}

			text {
				font-size: 26rpx;
			}

		}
	}
	.icon{
		color: red;
	}
</style>
